/**
 * Created by Administrator on 2017/2/15 0015.
 */
const userController = require('../controller/user-controller.js');
const userApi = {

    /**
     * 获取所有用户角色
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    getAllUserRoles (req, res) {
        let params = { queryOperation: req.query.queryOperation };
        return userController.getAllUserRoles(params).then((data) => {
            res.json({
                message: data.message,
                result: data.result
            });
        }, (error) => {
            res.status(error.status).json({
                message: error.message
            });
        });
    },

    /**
     * 根据角色获取所有用户
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    getAllUsersByRole (req, res) {
        let role = `ROLE_${req.params.role}`,
            filterAccountPrefix = req.query.filterAccountPrefix || '',
            params = {role, filterAccountPrefix};

        return userController.getAllUsersByRole(params).then((data) => {
            res.json({
                message: 'ok',
                result: data
            });
        }, (error) => {
            res.status(error.status).json({
                message: error.message
            });
        });
    },

    /**
     * 根据角色和账号前缀，分页查询用户
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    getPaginationUsersByRole (req, res) {
        let role = req.params.role,
            pageSize = Number(req.params.pageSize),
            pageNumber = Number(req.params.pageNumber),
            filterAccountPrefix = req.query.filterAccountPrefix || '',
            params = { role, pageSize, pageNumber, filterAccountPrefix };

        if (!role || !pageSize || !pageNumber) {
            res.status(400).json({ message: `params missed.` });
            return
        }

        return userController.getPaginationUsersByRole(params).then((data) => {
            res.json({
                message: 'ok',
                result: data
            });
        }, (error) => {
            res.status(error.status).json({
                message: error.message
            });
        });
    },

    /**
     * 创建用户
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    createUser (req, res) {
        let { creatorFullName, creatorRoleName, email, enabled, fullName, hasRole, loginAccount, nickName, password, phone, portrait, portrait_url, rating, roleKey, subjects, teachingExperience } = req.body,
            params = { creatorFullName, creatorRoleName, email, enabled, fullName, hasRole, loginAccount, nickName, password, phone, portrait, portrait_url, rating, roleKey, subjects, teachingExperience };
        params.password = req.query.loginPassword;

        return userController.createUser(params).then((data) => {
            res.json(data);
        });
    },

    /**
     * 删除用户
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    deleteUser (req, res) {
        let deletingUserId = req.params.deletingUserId,
            params = { deletingUserId };
        return userController.deleteUser(params).then((data) => {
            res.json(data);
        }, (err) => {
            res.status(err.status).json({ message: err.message});
        });
    },

    /**
     * 更新用户信息
     * @param req
     * @param res
     * @returns {Promise.<T>}
     */
    updateUserInfo (req, res) {
        return userController.updateUserInfo(req, res).then((data) => {
            res.json(data);
        });
    }
};


module.exports = userApi;